package com.ebeitech.library.ui.dialog;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;

import com.ebeitech.library.util.SPManager;

/**
 * Created by alice on 2017/12/2.
 */
public class DialogActivity extends FragmentActivity {

	public static CommonAlertDialogFragment mDialogFragment;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		int count = SPManager.getInstance().get("dialogCount", 0);
		count++;
		SPManager.getInstance().put("dialogCount", count);
		registerBroadCast();
		if (mDialogFragment != null) {
			CommonAlertDialogFragment mDialogFragmentN = mDialogFragment.getClone();
			mDialogFragmentN.setDismissClickListener(new DialogInterface.OnDismissListener() {
				@Override
				public void onDismiss(DialogInterface dialog) {
					finish();
				}
			});
			mDialogFragmentN.show(getSupportFragmentManager(), "DialogActivity");
		} else {
			finish();
		}
	}

	private void registerBroadCast() {
		IntentFilter myIntentFilter = new IntentFilter();
		myIntentFilter.addAction("ACTION_EXIT_DIALOG");
		registerReceiver(mBroadcastReceiver, myIntentFilter);
	}

	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if ("ACTION_EXIT_DIALOG".equals(action)) {
				finish();
			}
		}
	};

	@Override
	public void onBackPressed() {

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mDialogFragment = null;
		int count = SPManager.getInstance().get("dialogCount", 0);
		count--;
		SPManager.getInstance().put("dialogCount", count);
		unregisterReceiver(mBroadcastReceiver);
	}

}
